//
//  BaseViewController.m
//  HealthCompass
//
//  Created by Harry on 15/4/8.
//  Copyright (c) 2015年 bhrw. All rights reserved.
//

#import "BaseViewController.h"
#import "NSString+function.h"

@interface BaseViewController ()

// 雪花动画
@property (strong, nonatomic) CADisplayLink *displayLink;

@end

@implementation BaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)]) {
        [self setEdgesForExtendedLayout:UIRectEdgeNone];
    }
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:self action:@selector(goBack)];
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    self.navigationController.navigationBar.barTintColor = MainColor;
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = BackColor;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//如果是通过新的Navigation Controller Present需用如下方法
-(void)setNavigationTitle:(NSString *)title{
    self.navigationItem.title=title;
}

-(void)setNavigationTitle:(NSString *)title Color:(UIColor *)color{
    if (self.navigationItem) {
        UILabel *labelTitle=[[UILabel alloc] initWithFrame:CGRectZero];
        labelTitle.text=title;
        labelTitle.font=[UIFont systemFontOfSize:18.0];
        labelTitle.textColor=color;
        labelTitle.textAlignment=NSTextAlignmentCenter;
        self.navigationItem.titleView=labelTitle;
        [labelTitle sizeToFit];
        [self.navigationItem.titleView sizeToFit];
    }
}


-(UIBarButtonItem*)addLeftNavigationBarButtonImage:(NSString *)image Title:(NSString *)title Target:(id)target Action:(SEL)action{
    UIButton *btn=[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 30)];
    if (image!=nil) {
        btn.size = CGSizeMake([UIImage imageNamed:image].size.width+[title getTextWidthfont:[UIFont systemFontOfSize:18] labelHeight:30]+10, [UIImage imageNamed:image].size.height) ;
    } else {
        btn.size = CGSizeMake([title getTextWidthfont:[UIFont systemFontOfSize:18] labelHeight:30]+10, 30) ;
    }
    if (image) {
        [btn setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
    }
    [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:14];
    [btn setTitle:title forState:UIControlStateNormal];
    if ([[[UIDevice currentDevice] systemVersion] floatValue]>=11.0) {
//        btn.contentEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
//        btn.imageEdgeInsets = UIEdgeInsetsMake(0, -15, 0, 0);
//        btn.titleEdgeInsets = UIEdgeInsetsMake(0, -15, 0, 0);
    }
    [btn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    UIBarButtonItem *leftItem=[[UIBarButtonItem alloc] initWithCustomView:btn];
    if ([[[UIDevice currentDevice] systemVersion] floatValue]>7.0) {
        UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
        negativeSpacer.width = -20;
        self.navigationItem.leftBarButtonItems=@[negativeSpacer,leftItem];
    } else {
        self.navigationItem.leftBarButtonItem=leftItem;
    }
    return leftItem;
}

-(UIBarButtonItem*)addRightNavigationBarButtonImage:(NSString *)image Title:(NSString *)title Target:(id)target Action:(SEL)action{
    UIButton *btn=[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
    if (image!=nil) {
        if (title.length>0) {
            btn.size = CGSizeMake([UIImage imageNamed:image].size.width+[title getTextWidthfont:[UIFont systemFontOfSize:18] labelHeight:30]+10,[UIImage imageNamed:image].size.height) ;
        } else {
            btn.size = CGSizeMake(30, 30);
        }
    } else {
        btn.size = CGSizeMake([title getTextWidthfont:[UIFont systemFontOfSize:18] labelHeight:30]+10, 30) ;
    }
    if (image) {
        [btn setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
    }
    [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:14];
    [btn setTitle:title forState:UIControlStateNormal];
    if ([[[UIDevice currentDevice] systemVersion] floatValue]>=11.0) {
//        btn.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -15);
//        btn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -15);
//        btn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -15);
    }
    [btn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    UIBarButtonItem *rightItem=[[UIBarButtonItem alloc] initWithCustomView:btn];
    
    if ([[[UIDevice currentDevice] systemVersion] floatValue]>7.0) {
        UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
        negativeSpacer.width = -20;
        self.navigationItem.rightBarButtonItems=@[negativeSpacer,rightItem];
    } else {
        self.navigationItem.rightBarButtonItem=rightItem;
    }
    return rightItem;
}

- (void)endEdit {
    [self.view endEditing:YES];
}

- (void)goBack {
    NSArray *viewcontrollers=self.navigationController.viewControllers;
    if (viewcontrollers.count>1) {
        if ([viewcontrollers objectAtIndex:viewcontrollers.count-1]==self) {
            //push方式
            [self.navigationController popViewControllerAnimated:YES];
        }
    } else {
        //present方式
        [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    }
}

- (void)addLeftButtons:(NSArray<UIBarButtonItem *> *)buttonsArray {
    self.navigationItem.leftBarButtonItems = buttonsArray;
}

- (void)addRightButtons:(NSArray<UIBarButtonItem *> *)buttonsArray {
    self.navigationItem.rightBarButtonItems = buttonsArray;
}

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    
}


@end
